from osv import osv, fields
import datetime
class kdvn_budget_history(osv.osv):
    _name='kdvn.budget.history'
    _description="KDVN Budget History"
    
    def create_history(self,cr,uid,prj_id):            
            #cr.execute(" % prj_id)
            material = 0.0
            sub = 0.0
            site = 0.0
            admin =0.0
            profit = 0.0
            list_history = {'date':datetime.datetime.now().strftime('%Y-%m-%d'),
                            'status':'draft',
                            'material':0.0,
                            'labour':0.0,
                            'sub_contractor':0.0,
                            'site_expense':0.0,
                            'indirect_cost':0.0,
                            'business_profit':0.0,                  
                            'amount':0.0,
                            'project_id':prj_id}
            
            cr.execute("""Select 
                            bc.cat_code as cat_code,
                            sum(coalesce(budget_amount,0)) as amount
                        from 
                            kdvn_budget_data kbd
                        left join
                            account_budget_post abp on kbd.budget_id = abp.id
                        left join
                            budget_category bc on abp.categ_id = bc.id
                        where
                            kbd.project_id in (%s)
                        group by 
                            bc.cat_code
                        Union
                        Select
                            'indirect_cost' as cat_code,
                            coalesce(prj_budget_total,0)*sum(coalesce(bc.auto_rate,0))/100.0 as amount
                        from 
                            kdvn_project kp,budget_category bc
                        left join
                            budget_category bcp on bc.parent_id = bcp.id
                        where
                            bcp.cat_code='indirect_cost' and kp.id in (%s)
                        group by
                            kp.id,coalesce(prj_budget_total,0)
                        Union
                        Select
                            'business_profit' as cat_code,
                            prj_budget_total - subtotal -sum(coalesce(budget_amount,0)) as amount
                        from 
                            kdvn_budget_data kbd
                        left join
                            (Select 
                            kp.id,
                            coalesce(prj_budget_total,0) as prj_budget_total,
                            coalesce(prj_budget_total,0)*sum(coalesce(bc.auto_rate,0))/100.0 as subtotal
                            from 
                            kdvn_project kp,budget_category bc
                            left join
                            budget_category bcp on bc.parent_id = bcp.id
                            where
                            bcp.cat_code='indirect_cost'
                            group by
                            kp.id,coalesce(prj_budget_total,0)) vwindirect_toal on kbd.project_id = vwindirect_toal.id
                        where
                            kbd.project_id in (%s)
                        group by 
                            subtotal,prj_budget_total""" %(prj_id,prj_id,prj_id))
            
            for cat_code,amount in cr.fetchall():
                list_history['amount'] = list_history['amount'] + amount
                list_history[cat_code] = amount

            new_id = self.create(cr,uid,list_history)
            return new_id
        
    _order = "date desc"
    _columns={
         'project_id':fields.many2one('kdvn.project','Project'),
         'status':fields.selection([('draft','Draft'),('accepted','Accepted'),('rejected','Rejected')],'Status'),     
         'date':fields.date('Date',readonly=True),
         'material':fields.float('Material',readonly=True),
         'sub_contractor':fields.float('Sub-Contractor',readonly=True),
         'labour':fields.float('Labour',readonly=True),
         'site_expense':fields.float('Site-Expense',readonly=True),
         'indirect_cost':fields.float("Indirect Cost (24%)",readonly=True),
         'business_profit':fields.float('Business Profit',readonly=True),                  
         'amount':fields.float('Total',readonly=True)
         }
    _defaults={
            'status':lambda *x:'draft'
               }
kdvn_budget_history()
